<script language="JavaScript">
$('#prec_position').select2();
$('#usage_tj').select2();
$('#usage_pc').select2();
$('#usage_md').select2();
$.post('{{ urlfor "ProductsController.GetOptions"}}', {'fields':['cf_presc_usage_pc', 'cf_presc_usage_tj']},  function(res){
    set_options_list('usage_pc',res.data.cf_presc_usage_pc );
    set_options_list('usage_tj',res.data.cf_presc_usage_tj );

    //调取父页面用户数据
    var member_data = parent.sel_member;
    if(member_data.archives) {
        var main_obj = $('.userinfo');
        var uinfo = member_data.archives ;
        main_obj.find('.name').html(uinfo.Realname);
        main_obj.find('.sex').html(uinfo.Sex);
        main_obj.find('.age').html(uinfo.Age+'岁');
        main_obj.find('.mobile').html(uinfo.Mobile);
        main_obj.find('.birthday').html(uinfo.Birthday);

        $('[name="save_archives_id"]').val(uinfo.Id);
        $('[name="save_guahao_id"]').val(member_data.id);
        $.post('{{ urlfor "PrescController.Diagnosis" }}',{'guahao_id':member_data.id, 'archives_id':uinfo.Id}, function (res1) {
            if(res1.code == 0)  {
                var content = res1.data.content;
                $.each(content.split(","), function(idx, diagnosis){
                    addDiagnosisList(diagnosis);
                });
            }
        },'json');
    }
});

//设置下拉框值
function set_options_list(target, datas) {
    if(!datas) {
        return false;
    }
    var opt = ['<option value=""></option>'];
    $.each(datas, function(idx, val){
        opt.push('<option value="'+ val.val +'">'+ val.val +'</option>');
    });
    $('#'+target).html(opt.join(''));
}
setEnterEvevtToObject('content', showInputQueryList);
setEnterEvevtToObject('diagnosis', showInputQueryList);
setEnterEvevtToObject('position', showInputQueryList);

//判断该挂号是否已有诊断

//加入诊断
$('#save-diagnosis').click(function(){
    var diagnosis = $('#diagnosis').val();
    addDiagnosisList(diagnosis);
});
//删除诊断
$('#del-diagnosis').click(function () {
    var tables = $('#prescription_diagnosis');
    tables.find('input').each(function(){
        if($(this).prop('checked')) {
            $(this).parent().parent().remove();
        }
    });
    return false;
});
//加入常用诊断
$('#add-diagnosis-often').click(function () {
    var tables = $('#prescription_diagnosis');
    var tit_list = [];
    tables.find('input').each(function(){
        if($(this).prop('checked')) {
            tit_list.push($(this).parent().parent().find('.name').html());
        }
    });
    if(tit_list.length <= 0) {
        layer.msg('请选择需保存的项目');return false;
    }
    $.post('{{ urlfor "InputDiagnosisController.DiagnosisAddOften" }}', {'title':tit_list.join(',')},function (res) {
        if(res.code == 1) {
            layer.msg(res.msg);return false;
        }
        layer.msg('添加成功');
    })
});
//检查药品TAB
$('.project-type li').click(function () {
    $('.project-type li').removeClass('am-active');
    $(this).addClass('am-active');
    var type = $(this).index() == 0 ? 'inspects' : 'drug';
    checkInputDisabled(type);
    getOftenProject(type);
});

//医嘱暂存
$('#save-presc').click(function(){
    var presc_type = $('[name="presc_type"]').val();
    var project_id = parseInt($('[name="project_id"]').val());
    if(presc_type == '' || project_id == 0) {
        return false;
    }
    if(presc_type == 'drug' && $('[name="diagnosis_input"]').length<=0) {
        layer.alert('药品必须下达诊断');return false;
    }
    var table_box = $('#presc_table_list');
    if(table_box.find('[data-projectid="'+ project_id +'"]').length > 0 ){
        table_box.find('[data-projectid="'+ project_id +'"]').remove();  //已存在则移除掉
    }
    var content = $('#content').val();
    var prec_position = checkIsNull($('#prec_position').val());
    var entrust = $('[name="entrust"]').val();
    var usage_tj = checkIsNull($('#usage_tj').val());
    var usage_pc = checkIsNull($('#usage_pc').val());
    var usage_md = checkIsNull($('#usage_md').val());
    var dosage_jl = $('[name="dosage[jl]"]').val();
    var dosage_ts = $('[name="dosage[ts]"]').val();
    var dosage_zl = $('[name="dosage_zl"]').val();
    var content_number = $('[name="content_number"]').val();
    var content_spec = $('[name="content_spec"]').val();
    var unit = $('.presc_unit').html();
    var table_tr_list = [];
    table_tr_list.push('<tr data-projectid="'+ project_id +'">');
    table_tr_list.push('<input type="hidden" name="save_project_id" value="'+ project_id +'">');
    table_tr_list.push('<input type="hidden" name="save_project_type" value="'+ presc_type +'">');
    table_tr_list.push('<input type="hidden" name="save_content" value="'+ content +'">');
    table_tr_list.push('<input type="hidden" name="save_position" value="'+ prec_position +'">');
    table_tr_list.push('<input type="hidden" name="save_entrust" value="'+ entrust +'">');
    table_tr_list.push('<input type="hidden" name="save_usage_tj" value="'+ usage_tj +'">');
    table_tr_list.push('<input type="hidden" name="save_usage_pc" value="'+ usage_pc +'">');
    table_tr_list.push('<input type="hidden" name="save_usage_md" value="'+ usage_md +'">');
    table_tr_list.push('<input type="hidden" name="save_dosage_jl" value="'+ dosage_jl +'">');
    table_tr_list.push('<input type="hidden" name="save_dosage_ts" value="'+ dosage_ts +'">');
    table_tr_list.push('<input type="hidden" name="save_dosage_zl" value="'+ dosage_zl +'">');
    table_tr_list.push('<input type="hidden" name="save_content_number" value="'+ content_number +'">');
    table_tr_list.push('<input type="hidden" name="save_content_spec" value="'+ content_spec +'">');
    table_tr_list.push('<input type="hidden" name="save_unit" value="'+ unit +'">');
    table_tr_list.push('<td><a href="javascript:;" style="color:red" onclick="$(this).parent().parent().remove();return false;"><i class="am-icon-trash"></i></a></td>');
    table_tr_list.push('<td>'+ content +'</td>');
    table_tr_list.push('<td>'+ dosage_zl +'</td>');
    table_tr_list.push('<td>'+ unit +'</td>');
    table_tr_list.push('<td>'+ (prec_position ? prec_position +'，':'')+ showUsageFormat(usage_tj, usage_pc, dosage_ts) +'</td>');
    table_tr_list.push('<td>'+ (dosage_jl?'每次'+dosage_jl+'滴':'') +'</td>');
    table_tr_list.push('<td>'+ entrust +'</td>');
    table_tr_list.push('<td>'+ usage_md +'</td>');
    table_tr_list.push('</tr>');
    table_box.append(table_tr_list.join(''));
    presc_input_init();
});

//修改项目
$('#presc_table_list').on('dblclick', 'tr',function(){
    var target_info = getPrescDatas(this);
    console.log(target_info);
    var presc_type =  target_info.project_type;
    var project_id =  target_info.project_id;
    var content = target_info.content;
    var prec_position = target_info.position;
    var entrust = target_info.entrust;
    var usage_tj =target_info.usage_tj;
    var usage_pc = target_info.usage_pc;
    var usage_md = target_info.usage_md;
    var dosage_jl = target_info.dosage_jl;
    var dosage_ts = target_info.dosage_ts;
    var dosage_zl =target_info.dosage_zl;
    var unit = target_info.unit;
    var content_number = target_info.content_number;
    var content_spec = target_info.content_spec;
    $('#content').val(content);
    $('[name="presc_type"]').val(presc_type);
    $('[name="project_id"]').val(project_id);
    $('#prec_position').val(prec_position).trigger('change');
    $('[name="entrust"]').val(entrust);
    $('#usage_tj').val(usage_tj).trigger('change');
    $('#usage_pc').val(usage_pc).trigger('change');
    $('#usage_md').val(usage_md).trigger('change');
    $('[name="dosage[jl]"]').val(dosage_jl);
    $('[name="dosage[ts]"]').val(dosage_ts);
    $('[name="dosage_zl"]').val(dosage_zl);
    $('[name="content_number"]').val(content_number);
    $('[name="content_spec"]').val(content_spec);

    $('.presc_unit').html(unit);
    checkInputDisabled(presc_type);
});
//发送医嘱
$('#send-presc').click(function () {
    var table_box = $('#presc_table_list');
    if(table_box.find('tr').length <= 0) {
        layer.msg('请填写医嘱内容');return false;
    }
    var save_archives_id = parseInt($('[name="save_archives_id"]').val());
    var save_guahao_id = parseInt($('[name="save_guahao_id"]').val());
    if(!save_archives_id || !save_guahao_id) {
        layer.msg('参数错误');return false;
    }
    //获取医嘱
    var save_datas = [];
    $.each(table_box.find('tr'), function (idx, val) {
        save_datas.push(getPrescDatas(this));
    });
    //获取诊断
    var prescription_diagnosis = $('#prescription_diagnosis');
    var diagnosis = [];
    $.each(prescription_diagnosis.find('tr'), function (idx, val) {
        diagnosis.push($(this).find('[name="diagnosis_input"]').val());
    });
    var save = {'archives_id':save_archives_id, 'guahao_id':save_guahao_id, 'save_datas':JSON.stringify(save_datas), 'diagnosis':diagnosis.join(',')};

    $.post('{{ urlfor "PrescController.SavePrescription" }}', save, function (res) {
        if(res.code == 1) {
            layer.alert(res.msg);return false;
        }
        if(typeof parent.presc_callback == 'function') {
            parent.presc_callback();
        }
        parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭窗口
    }, 'json');
});
var often_project_datas;
//双击常用项目
$('#often_project').on('dblclick', 'tr', function(){
    var project_type = $(this).data('type');
    var index = $(this).data('index');
    if(project_type == 'drug') {
        presc_content_callback(project_type, often_project_datas[index].inventory);
    } else {
        presc_content_callback(project_type, often_project_datas[index].inspects);
    }
});
//获取医嘱记信息
function getPrescDatas(tar) {
    var presc_type =  $(tar).find('[name="save_project_type"]').val();
    var project_id =  parseInt($(tar).find('[name="save_project_id"]').val());
    var content = $(tar).find('[name="save_content"]').val();
    var prec_position = $(tar).find('[name="save_position"]').val();
    var entrust = $(tar).find('[name="save_entrust"]').val();
    var usage_tj = $(tar).find('[name="save_usage_tj"]').val();
    var usage_pc = $(tar).find('[name="save_usage_pc"]').val();
    var usage_md = $(tar).find('[name="save_usage_md"]').val();
    var dosage_jl = $(tar).find('[name="save_dosage_jl"]').val();
    var dosage_ts = $(tar).find('[name="save_dosage_ts"]').val();
    var dosage_zl = parseInt($(tar).find('[name="save_dosage_zl"]').val());
    var unit = $(tar).find('[name="save_unit"]').val();
    var content_number = $(tar).find('[name="save_content_number"]').val();
    var content_spec = $(tar).find('[name="save_content_spec"]').val();
    return {
        'project_type':presc_type,
        'project_id':project_id,
        'content':content,
        'content_number':content_number,
        'content_spec':content_spec,
        'position':prec_position?prec_position:'',
        'entrust':entrust,
        'usage_tj':usage_tj?usage_tj:'',
        'usage_pc':usage_pc?usage_pc:'',
        'usage_md':usage_md?usage_md:'',
        'dosage_jl':dosage_jl?dosage_jl:'',
        'dosage_ts':dosage_ts?dosage_ts:'',
        'dosage_zl':dosage_zl?dosage_zl:'',
        'unit':unit
    };
}
//调用常用项目
function getOftenProject(project_type) {
    var often_project = $('#often_project');
    $.post('{{ urlfor "PrescController.OftenProject" }}', {'project_type':project_type}, function(res) {
        if(res.code == 0) {
            often_project.html('');
            var table_tr = [];
             $.each(res.data, function (idx,val) {
                 table_tr.push('<tr data-type="'+ val.project_type +'" data-index="'+ idx +'">');
                 if(val.project_type == 'inspects') {  //检查
                     //table_tr.push('<input type="hidden" name="often_project_id" value="'+ val.inspects.Id +'"><input type="hidden" name="often_project_type" value="inspects">');
                     table_tr.push('<td>'+ val.inspects.ProductName +'</td>');
                     table_tr.push('<td>'+ val.inspects.Pinyin +'</td>');
                 } else {  //药品
                     //table_tr.push('<input type="hidden" name="often_project_id" value="'+ val.inspects.Id +'"><input type="hidden" name="often_project_type" value="inspects">');
                     table_tr.push('<td>'+ val.inventory.Products.FullName+val.inventory.Products.Spec +'</td>');
                     table_tr.push('<td>'+ val.inventory.Products.Pinyin +'</td>');
                 }
                 table_tr.push('<td>'+ val.use_number +'</td>');
                 table_tr.push('</tr>');
             });
            often_project.html(table_tr.join(''));
            often_project_datas = res.data;
        }
    }, 'json');
}
//默认显示检查常用项目
getOftenProject('inspects');

//加入诊断
function addDiagnosisList(diagnosis) {
    if(diagnosis == '') {
        return false;
    }
    var tables = $('#prescription_diagnosis');
    if(tables.find('[value="'+ diagnosis + '"]').length > 0) {
        return false;
    }
    var tr = [];
    tr.push('<tr>');
    tr.push('<td><input type="checkbox" name="sel_diagnosis"></td>');
    tr.push('<td class="name">'+ diagnosis +'<input type="hidden" value="'+ diagnosis +'"  name="diagnosis_input"/> </td>');
    tr.push('</tr>');
    $('#prescription_diagnosis').append(tr.join(''));
    $('#diagnosis').val('');
}
//医嘱初始化
function presc_input_init() {
    $('input[name="dosage[jl]"]').val('');//剂量
    $('input[name="dosage[ts]"]').val(''); //天数
    $('input[name="entrust"]').val(''); //医生嘱托
    $('.presc_unit').html('次');//设置单位
    $('[name="presc_type"]').val('');
    $('[name="project_id"]').val(0);
    $('#content').val('');
    $('#prec_position').val('').trigger('change');
    $('#usage_tj').val('').trigger('change');
    $('#usage_pc').val('').trigger('change');
    $('#usage_md').val('').trigger('change');
    $('[name="content_number"]').val('');
    $('[name="content_spec"]').val('');
}

//诊断返回
function presc_diagnosis_callback(title) {
    $('#diagnosis').val(title);
    addDiagnosisList(title);
}
//医嘱可写状态
function checkInputDisabled(t) {
    if(t == 'drug') {
        $('.drug').attr('disabled',false);
    } else {
        $('.drug').attr('disabled',true);
    }
}
//医嘱内容返回（类型，数据）
function presc_content_callback(type, data) {
    presc_input_init();
    var dosage_jl = '';//剂量
    var dosage_ts = ''; //天数
    var usage_tj = '';
    var usage_pc = '';
    var usage_md = '';
    var unit = '次';
    var content = '';
    var position = '';//部位
    var presc_type = ''; //医嘱类型
    var project_id = 0;
    var content_number ='';
    var content_spec = '';
    if(type == 'drug') {  //药品
        //设置默认值
        dosage_jl = 1;
        dosage_ts = 1;
        unit = data.Products.Unit;
        content = data.Products.FullName + data.Products.Spec;
        content_number = data.Products.Number;
        content_spec = data.Products.Spec;
        presc_type =  'drug';
        project_id = data.Id;
        usage_tj = '滴双眼';
        usage_pc = '每日3次';
    } else {
        unit = data.Unit;
        content = data.ProductName;
        presc_type =  'inspects';
        project_id = data.Id;
        usage_pc = '一次性';
        content_number = data.Number;
        content_spec = '';
    }
    checkInputDisabled(type);
    $('input[name="dosage[jl]"]').val(dosage_jl);//剂量
    $('input[name="dosage[ts]"]').val(dosage_ts); //天数

    $('#prec_position').val(position).trigger('change');
    $('#usage_tj').val(usage_tj).trigger('change');
    $('#usage_pc').val(usage_pc).trigger('change');
    $('#usage_md').val(usage_md).trigger('change');

    $('input[name="entrust"]').val(''); //医生嘱托
    $('.presc_unit').html(unit);//设置单位
    $('[name="presc_type"]').val(presc_type);
    $('[name="project_id"]').val(project_id);
    $('[name="content_number"]').val(content_number);
    $('[name="content_spec"]').val(content_spec);
    $('#content').val(content);
}
function checkIsNull(value) {
    return value==''||value == null ? '' :value;
}
</script>